package com.vcaresolution.logistic.service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.vcaresolution.logistic.form.BillofladingForm;
import com.vcaresolution.logistic.pojo.Billoflading;
import com.vcaresolution.logistic.to.BillofladingTO;
import com.vcaresolution.util.Utils;

@Service
public class BillofladingServiceImpl extends BaseService implements BillofladingService{

	private Billoflading populateBillofladingPojo(BillofladingForm billofladingForm){
		Billoflading billoflading = new Billoflading();
		BeanUtils.copyProperties(billofladingForm, billoflading);
		
		return billoflading;
	}
	
	@Override
	@Transactional
	public void add(BillofladingForm billofladingForm) {
		Billoflading billoflading = populateBillofladingPojo(billofladingForm);
		
		billoflading.setCreationdate(Utils.getCurrentDate());
						
		getDaoFactory().getUtilityDAO().add(billoflading);
	}

	@Override
	@Transactional
	public void update(BillofladingForm billofladingForm) {
		Billoflading billoflading = populateBillofladingPojo(billofladingForm);
		
		getDaoFactory().getUtilityDAO().update(billoflading);
	}

	@Override
	@Transactional
	public void delete(BillofladingForm billofladingForm) {
		getDaoFactory().getUtilityDAO().delete(populateBillofladingPojo(billofladingForm));
	}

	@Override
	@Transactional
	public BillofladingTO findById(Serializable blID) {
		Billoflading billoflading = (Billoflading) getDaoFactory().getUtilityDAO().findById(Billoflading.class,blID);
		
		BillofladingTO billofladingTO = new BillofladingTO();
		BeanUtils.copyProperties(billoflading, billofladingTO);
		
		return billofladingTO; 
	}

	@Override
	@Transactional
	public List<BillofladingTO> findByAll() {
		List<Billoflading> billofladingList = getDaoFactory().getUtilityDAO().findByAll(Billoflading.class);
		
		List<BillofladingTO> billofladingFormList = new ArrayList<BillofladingTO>();
		
		if( billofladingList != null && !billofladingList.isEmpty() ){
			BillofladingTO billofladingForm = null;
			for(Billoflading billoflading: billofladingList){
				billofladingForm = new BillofladingTO();
				
				BeanUtils.copyProperties(billoflading, billofladingForm);
				billofladingFormList.add(billofladingForm);
			}
		}	
		return billofladingFormList;
	}
}